package view;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;
import javax.swing.border.EtchedBorder;
import javax.swing.JScrollPane;

public class MainView {

	private JFrame frame;
	private CardLayout card;
	private JPanel view;

	/**
	 * Create the application.
	 */
	public MainView() {
		frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// initialize();
		frame.add(new PanelLoggin(this));
		frame.setBounds(GraphicsEnvironment.getLocalGraphicsEnvironment()
				.getMaximumWindowBounds());
		frame.setVisible(true);
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		ButtonGroup group = new ButtonGroup();

		JToolBar toolBar = new JToolBar();
		toolBar.setRollover(true);
		toolBar.setBorder(new EtchedBorder(EtchedBorder.RAISED, null, null));
		toolBar.setFloatable(false);
		frame.getContentPane().add(toolBar, BorderLayout.NORTH);

		JToggleButton tglbtnLpHBn = new JToggleButton("Hóa đơn");
		tglbtnLpHBn.setFont(new Font("Tahoma", Font.PLAIN, 15));
		tglbtnLpHBn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				show("hoadon");
			}
		});
		tglbtnLpHBn.setIcon(new ImageIcon(MainView.class
				.getResource("/images/bill.png")));
		toolBar.add(tglbtnLpHBn);
		tglbtnLpHBn.setFocusable(false);
		group.add(tglbtnLpHBn);
		tglbtnLpHBn.setSelected(true);

		JToggleButton tglbtnLpHMua = new JToggleButton("Nhân viên");
		tglbtnLpHMua.setFont(new Font("Tahoma", Font.PLAIN, 15));
		tglbtnLpHMua.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				show("baocao");
			}
		});
		tglbtnLpHMua.setIcon(new ImageIcon(MainView.class
				.getResource("/images/employee.png")));
		toolBar.add(tglbtnLpHMua);
		tglbtnLpHMua.setFocusable(false);
		group.add(tglbtnLpHMua);

		JToggleButton tglbtnThngK = new JToggleButton("Thống kê");
		tglbtnThngK.setFont(new Font("Tahoma", Font.PLAIN, 15));
		tglbtnThngK.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				show("thongke");
			}
		});
		tglbtnThngK.setIcon(new ImageIcon(MainView.class
				.getResource("/images/chart.png")));
		toolBar.add(tglbtnThngK);
		tglbtnThngK.setFocusable(false);
		group.add(tglbtnThngK);

		JToggleButton tglbtnKhoHng = new JToggleButton("Kho hàng");
		tglbtnKhoHng.setIcon(new ImageIcon(MainView.class
				.getResource("/images/store.png")));
		tglbtnKhoHng.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				show("khohang");
			}
		});
		tglbtnKhoHng.setFont(new Font("Tahoma", Font.PLAIN, 15));
		tglbtnKhoHng.setFocusable(false);
		toolBar.add(tglbtnKhoHng);
		group.add(tglbtnKhoHng);

		JToggleButton tglbtnLuTr = new JToggleButton("Lưu trữ");
		tglbtnLuTr.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				show("luutru");
			}
		});
		tglbtnLuTr.setFocusable(false);
		tglbtnLuTr.setIcon(new ImageIcon(MainView.class
				.getResource("/images/report.png")));
		tglbtnLuTr.setFont(new Font("Tahoma", Font.PLAIN, 15));
		toolBar.add(tglbtnLuTr);
		group.add(tglbtnLuTr);

		JToggleButton tglbtnHThng = new JToggleButton("Hệ thống");
		tglbtnHThng.setFont(new Font("Tahoma", Font.PLAIN, 15));
		tglbtnHThng.setIcon(new ImageIcon(MainView.class
				.getResource("/images/sys.png")));
		toolBar.add(tglbtnHThng);
		tglbtnHThng.setFocusable(false);
		group.add(tglbtnHThng);
		tglbtnHThng.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				show("hethong");
			}
		});

		JPanel panel = new JPanel();
		toolBar.add(panel);
		panel.setLayout(new BorderLayout(0, 0));

		JLabel lblIcon = new JLabel("");
		lblIcon.setIcon(new ImageIcon(MainView.class
				.getResource("/images/icon.png")));
		panel.add(lblIcon, BorderLayout.EAST);

		view = new JPanel();
		frame.getContentPane().add(view, BorderLayout.CENTER);
		card = new CardLayout(0, 0);
		view.setLayout(card);

		JTabbedPane hoaDon = new JTabbedPane(JTabbedPane.TOP);
		hoaDon.setFocusable(false);
		view.add(hoaDon, "hoadon");

		PanelBan banHang = new PanelBan();
		hoaDon.addTab(
				"Bán hàng",
				new ImageIcon(MainView.class.getResource("/images/sellup.png")),
				banHang, null);

		PanelMua muaHang = new PanelMua();
		hoaDon.addTab(
				"Mua hàng",
				new ImageIcon(MainView.class
						.getResource("/images/selldown.png")), muaHang, null);

		JPanel baoCao = new PanelNhanVien();
		view.add(baoCao, "baocao");

		JPanel thongKe = new PanelThongKe();
		view.add(thongKe, "thongke");

		JPanel khoHang = new PanelKhoHang();
		view.add(khoHang, "khohang");

		JScrollPane luuTru = new JScrollPane();
		luuTru.setViewportView(new PanelLuuTru());
		view.add(luuTru, "luutru");

		JPanel heThong = new PanelSystem();
		view.add(heThong, "hethong");
	}

	public void show(String name) {
		card.show(view, name);
	}

	public void show() {
		frame.getContentPane().removeAll();
		initialize();
	}

}
